package com.rp.configure.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.NullIs;
import com.rp.configure.entity.Alarm;
import com.rp.configure.service.AlarmService;
import com.rp.system.user.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

import static java.lang.Thread.sleep;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-07-17
 */
@RestController
@RequestMapping("/alarm")
public class AlarmController {
    @Autowired
    AlarmService alarmService;
    @Autowired
    SysUserService sysUserService;
    @PostMapping("/insert")
     public ResultDTO insert(@RequestBody Alarm alarm) {

        Object json = JSON.toJSON(alarm.getWeek());
        alarm.setWeek(json.toString());
        Object json1 = JSON.toJSON(alarm.getOperationId());
        alarm.setOperationId(json1.toString());
      return ResultDTO.tf(alarmService.save(alarm));
    }
    @PutMapping("/update")
    @ApiOperation(value = "修改报警信息")
    public ResultDTO update(@RequestBody Alarm alarm) {
        Object json = JSON.toJSON(alarm.getWeek());
        alarm.setWeek(json.toString());
        Object json1 = JSON.toJSON(alarm.getOperationId());
        alarm.setOperationId(json1.toString());
        return ResultDTO.tf(alarmService.updateById(alarm));
    }
    @GetMapping("/list")
    @ApiOperation(value = "Alarm根据据用户id查询所有报警规则")
    public ResultDTO<List<Alarm>> list(@RequestParam(value = "userId",required = false) String userId,
                                       @RequestParam(value = "type",required = false) Integer type) {
        List<Alarm> list;

            QueryWrapper<Alarm> wrapper = new QueryWrapper<>();
        if (!NullIs.isEmpty(userId)){
            wrapper.eq("user_id",userId);}
        if (!NullIs.isEmpty(type)){
            wrapper.eq("type",type);}
            list = alarmService.list1(wrapper);
        return ResultDTO.success().data(list);
    }
    @GetMapping("/page")
    @ApiOperation(value = "Alarm更据用户id查询所有报警规则")
    public ResultDTO<IPage<Alarm>> list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                        @RequestParam(value = "userId",required = false) String userId) {
        IPage<Alarm> list;
            QueryWrapper<Alarm> wrapper = new QueryWrapper<>();
            if (!NullIs.isEmpty(userId)){
            wrapper.eq("user_id",userId);}
            list = alarmService.page1(new Page<Alarm>(pageNum, pageSize),wrapper);

        return ResultDTO.success().data(list);
    }
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id) {
        return ResultDTO.tf(alarmService.removeById(id));
    }
    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids) {
        return ResultDTO.tf(alarmService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
